package com.ryujung.binary_tree.leetCode_226;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }

    @Override
    public String toString() {
        return "TreeNode{" +
                "val=" + val +
                ",\n left=" + left +
                ",\n right=" + right +
                '}';
    }
}

class Solution {
    /**
     * 思路：
     * 递归调换左右子树
     */
    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        TreeNode l = root.left;
        TreeNode r = root.right;
        root.left = invertTree(r);
        root.right = invertTree(l);
        return root;
    }
    // 时间复杂度：O(n) 需要完整遍历二叉树
    // 空间复杂度：O(n) 递归的栈空间大小取决于树的高度，最坏情况为 n。

}